package com.ponhu.ea2.goodsCenter.provider.task;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ponhu.ea2.entity.LuxSkuInfo;
import com.ponhu.ea2.goodsCenter.provider.mapper.LuxSkuInfoMapper;
import com.ponhu.ea2.goodsCenter.provider.schedule.LuxPriceTask;
import com.ponhu.ea2.goodsCenter.provider.service.LuxSkuInfoService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author luyu
 * @create 2021-09-27 20:26
 */
public class LuxSkuInfoTask  implements Runnable {

    @Resource
    LuxSkuInfoMapper luxSkuInfoMapper;

    @Resource
    LuxSkuInfoService luxSkuInfoService;

    private List luxSkuList;

    private String time;


    public LuxSkuInfoTask(LuxSkuInfoService luxSkuInfoService, LuxSkuInfoMapper luxSkuInfoMapper, List skuList,String str) {
        this.luxSkuList=skuList;
        this.luxSkuInfoMapper = luxSkuInfoMapper;
        this.luxSkuInfoService = luxSkuInfoService;
        this.time=str;
    }

    @Override
    public void run() {
        process(luxSkuList);
    }

    public void process(List list){
        List<LuxSkuInfo> luxSkuinfoList = new ArrayList();
        // 遍历list,检查sku是否存在,如果不存在则执行新增
        for (int i = 0; i < list.size(); i++) {
            Map linkmap = (Map) list.get(i);
            // 检查sku是否存在,如果不存在则调用新增
            LuxSkuInfo luxSkuInfo = luxSkuInfoMapper.selectOne(
                    new QueryWrapper<LuxSkuInfo>().eq("sku", linkmap.get("sku").toString()));
            if(ObjectUtils.isEmpty(luxSkuInfo)){
                // 调用SKU详细字段查询
                String sku = linkmap.get("sku").toString();
                String url = "http://121.37.250.170:8085/goodsfind?sku="+sku;
                Map map = LuxPriceTask.sendGet(url);
                List pages = (ArrayList)map.get("pages");
                saveLuxSkuInfo(pages,time,luxSkuinfoList);// 保存sku详细信息
            }
        }
        if(!CollectionUtils.isEmpty(luxSkuinfoList)){
            luxSkuInfoService.saveBatch(luxSkuinfoList);
        }

    }


    public void saveLuxSkuInfo(List pages,String time,List list){
        if(!CollectionUtils.isEmpty(pages)){
            Map skuMap = (Map) pages.get(0);
            LuxSkuInfo luxSkuInfo = new LuxSkuInfo();
            luxSkuInfo.setSku(skuMap.get("sku")==null ? null : skuMap.get("sku").toString());
            luxSkuInfo.setBrand(skuMap.get("brand")==null ? null : skuMap.get("brand").toString());
            luxSkuInfo.setBrandId(skuMap.get("brand_id")==null ? null : skuMap.get("brand_id").toString());
            luxSkuInfo.setCategory(skuMap.get("category")==null ? null : skuMap.get("category").toString());
            luxSkuInfo.setColor(skuMap.get("color")==null ? null : skuMap.get("color").toString());
            luxSkuInfo.setColorId(skuMap.get("color_id")==null ? null : skuMap.get("color_id").toString());
            luxSkuInfo.setDetailUrl(skuMap.get("detail_url")==null ? null : skuMap.get("detail_url").toString());
            luxSkuInfo.setGrade(skuMap.get("grade")==null ? null : skuMap.get("grade").toString());
            luxSkuInfo.setGradeId(skuMap.get("grade_id")==null ? null : skuMap.get("grade_id").toString());
            luxSkuInfo.setMaterial(skuMap.get("material")==null ? null : skuMap.get("material").toString());
            luxSkuInfo.setMaterialId(skuMap.get("material_id")==null ? null : skuMap.get("material_id").toString());
            luxSkuInfo.setSeries(skuMap.get("series")==null ? null : skuMap.get("series").toString());
            luxSkuInfo.setSeriesId(skuMap.get("series_id")==null ? null : skuMap.get("series_id").toString());
            luxSkuInfo.setTitle(skuMap.get("title")==null ? null : skuMap.get("title").toString());
            luxSkuInfo.setVendor(skuMap.get("vendor")==null ? null : skuMap.get("vendor").toString());
            luxSkuInfo.setVendorId(skuMap.get("vendor_id")==null ? null : skuMap.get("vendor_id").toString());
            luxSkuInfo.setSize(skuMap.get("size2")==null ? null : skuMap.get("size2").toString());
            luxSkuInfo.setCreateTime(time);
            list.add(luxSkuInfo);
        }
    }

}
